/*
 * SquareModel.cpp
 *
 *  Created on: Dec 18, 2013
 *      Author: artech
 */

#include "SquareModel.h"
#include "Sdq.h"
#include "SdqException.h"

SquareModel::SquareModel(quint8 lineNb, quint8 columnNb) :
      _lineNb(lineNb), _columnNb(columnNb)
{
   _valueSet.reserve(GRID_SIZE);
   for (quint8 i = 1; i <= GRID_SIZE; ++i)
   {
      _valueSet.insert(i);
   }
   _square9Nb = _columnNb / 3 + _lineNb - _lineNb % 3;
   _baseLineNb = _lineNb - _lineNb % SQUARE_9_SIZE;
   _baseColumnNb = _columnNb - _columnNb % SQUARE_9_SIZE;
}

SquareModel::~SquareModel()
{
}

quint8 SquareModel::getLineNb() const
{
   return _lineNb;
}

quint8 SquareModel::getBaseLineNb() const
{
   return _baseLineNb;
}

quint8 SquareModel::getColumnNb() const
{
   return _columnNb;
}

quint8 SquareModel::getBaseColumnNb() const
{
   return _baseColumnNb;
}

quint8 SquareModel::getSquare9Nb() const
{
   return _square9Nb;
}

void SquareModel::setValue(quint8 value)
{
   _valueSet.clear();
   _valueSet.insert(value);
}

quint8 SquareModel::getValue() const
{
   if (doesValueBeFound())
   {
      return *_valueSet.begin();
   }
   throw SdqException(
         QString("More than one value possible for case %1-%2").arg(_lineNb).arg(
               _columnNb));
}

bool SquareModel::popValue(quint8 value)
{
   if (_valueSet.remove(value))
   {
      if (_valueSet.empty())
      {
         throw SdqException(
               QString("No more value possible for case %1-%2").arg(_lineNb).arg(
                     _columnNb));
      }
      return true;
   }
   return false;
}

bool SquareModel::doesValueBeFound() const
{
   return _valueSet.size() == 1;
}
